from usecaseapp import models
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User, Group


class UsecaseAdmin(admin.ModelAdmin):
    list_display = ('description', 'submitter', 'created_on')
    search_fields = ['description']


def add_to_post_group(modeladmin, request, queryset):
    addgroup = Group.objects.get(name="UsecasePosters")
    for obj in queryset:
        obj.groups.add(addgroup)

def remove_from_post_group(modeladmin, request, queryset):
    addgroup = Group.objects.get(name="UsecasePosters")
    for obj in queryset:
        obj.groups.remove(addgroup)

add_to_post_group.short_description = "Add to posters group"
remove_from_post_group.short_description = "Remove from posters group"
class MyUserAdmin(UserAdmin):
    list_filter = UserAdmin.list_filter + ('groups',)
    #fields = ('username', 'first_name', 'last_name', 'is_staff', 'is_active') 
    #fieldsets = ()

    list_display = UserAdmin.list_display + ('is_active', 'is_poster', )
    actions=[add_to_post_group, remove_from_post_group]

    def is_poster(self, obj):
        print obj.groups.values_list('id')
        postgroup =  Group.objects.get(name="UsecasePosters")
        print postgroup
        if len(obj.groups.values()) > 0 and (postgroup.id in obj.groups.values_list('id')[0]):
            return True
        else:
            return False

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

admin.site.register(models.Usecase, UsecaseAdmin)
